package utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ToolSet {
    /**
     * 向页面发送数据
     * @param resp
     * @param object
     * @throws IOException
     */
    public static void printWriter(HttpServletResponse resp, Object object) throws IOException {

        resp.setContentType("application/json;charset=utf-8");

        PrintWriter writer = resp.getWriter();

        ObjectMapper om = new ObjectMapper();

        om.writeValue(writer,object);

        writer.close();
    }

    /**
     * 向页面发送字符串
     * @param resp
     * @param str
     * @throws IOException
     */
    public static void writer(HttpServletResponse resp, String str) throws IOException {
        resp.setContentType("text/html;charset=utf-8");

        PrintWriter writer = resp.getWriter();

        writer.write(str);

        writer.close();
    }
}
